<template>
    <div class="article-page">
        <nav class="my-nav van-hairline--bottom">
            <a
            :class="{active:sorter=='weight_desc'}"
            @click="getData('weight_desc')"
              href="javascript:;"
              >推荐</a
            >
            <a
            :class="{active:sorter==null}"
            @click="getData(null)"
              href="javascript:;"
              >最新</a
            >
            <div class="logo"><img src="@/assets/logo.png" alt=""></div>
          </nav>
          <!-- 设置滚动条控制多个页面 -->
          <van-list 
          v-model="loading"
          :finished="finished"
          @load="onload"
          >
            <ArticleItem v-for="(item,index) in list" :key="index" :item="item"></ArticleItem>

          </van-list>

          
    </div>
  </template>
  
  <script>
  //局部组件的局部使用
//   import articleitem from '@/components/ArticleItem.vue'
import { getArticles } from '@/api/article';
  export default {
    name: 'article-page',
    data () {
      return {
        list:[],
        current:1,
        pageSize:10,
        loading:false,
        finished:false,
        sorter:'weight_desc'
      }
    },
    // components:{
    //     articleitem
    // },
    // async created(){
    //    const res= await getArticles({
    //     current:this.current,
    //     pageSize:this.pageSize
    //     })
    //     console.log(res)
    //     this.list=res.data.rows
    // },
    methods: {
      async onload(){
       const res= await getArticles({
        current:this.current,
        pageSize:this.pageSize,
        sorter:this.sorter
        })
        this.list.push(...res.data.rows)
        this.loading=false
        this.current++
        console.log(res)
        if(this.current>res.data.total){
          this.finished=true
        }

    },
    getData(value){
      this.sorter=value
      this.current=1
      this.list=[]
      this.finished=false
      this.loading=true
      this.onload()
    }
    }
  }
  </script>
  
  <style lang="less" scoped>
  .article-page {
    margin-bottom: 50px;
    margin-top: 44px;
    .my-nav {
      height: 44px;
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      z-index: 999;
      background: #fff;
      display: flex;
      align-items: center;
      > a {
        color: #999;
        font-size: 14px;
        line-height: 44px;
        margin-left: 20px;
        position: relative;
        transition: all 0.3s;
        &::after {
          content: '';
          position: absolute;
          left: 50%;
          transform: translateX(-50%);
          bottom: 0;
          width: 0;
          height: 2px;
          background: #222;
          transition: all 0.3s;
        }
        &.active {
          color: #222;
          &::after {
            width: 14px;
          }
        }
      }
      .logo {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        > img {
          width: 64px;
          height: 28px;
          display: block;
          margin-right: 10px;
        }
      }
    }
  }
  </style>